const path = require('path');
const express = require('express');
var captchapng = require('captchapng');
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017';
const dbName = 'studentmanager';

//获取注册页面
exports.getRegistPage = (req, res) => {
    res.sendFile(path.join(__dirname, "../static/view/regist.html"));
}
//注册处理逻辑
exports.getRegistInfo = (req, res) => {
    const username = req.body.username;
    const password = req.body.password;
    const result = {
        status: 0,
        message: '注册成功'
    }
    //1连接数据库
    MongoClient.connect(url, function (err, client) {
        const db = client.db(dbName);
        const collection = db.collection('student');
        //判断用户名是否存在
        collection.findOne({username: username}, (err, docs) => {
            if (docs != null) {
                result.status = 2;
                result.message = "用户名已经存在";
                client.close();
                res.send(result);
            } else {
                collection.insertOne(req.body, function (err, resu) {
                    if (resu) {
                        client.close();
                        res.send(result);
                    } else {
                        result.status = 1,
                            result.message = "注册失败"
                        client.close();
                        res.send(result);
                    }
                });
            }
        });
    });

}
//获取登录页面
exports.getLoginPage = (req, res) => {
    res.sendFile(path.join(__dirname, "../static/view/login.html"));
}
//获取验证码
exports.getVcode = (req,res) => {
        var vcode = parseInt(Math.random()*9000+1000);
        req.session.vcode = vcode;
        var p = new captchapng(80,30,vcode); // width,height,numeric captcha
        p.color(0, 0, 0, 0);  // First color: background (red, green, blue, alpha)
        p.color(80, 80, 80, 255); // Second color: paint (red, green, blue, alpha)
 
        var img = p.getBase64();
        var imgbase64 = new Buffer(img,'base64');
        res.writeHead(200, {
            'Content-Type': 'image/png'
        });
        res.end(imgbase64);
}
//登录逻辑处理
exports.getLoginInfo = (req,res)=>{
    var param = req.body
    const result = {
        status:0,
        message:'登录成功'
    }
    if (param.vcodeId != req.session.vcode) {
        result.status = 1;
        result.message ="验证码错误"
        res.send(result);
        return ;
    }
       //1连接数据库
       MongoClient.connect(url, function (err, client) {
        const db = client.db(dbName);
        const collection = db.collection('student');
        //判断用户名是否存在
        collection.findOne({username: param.username,password:param.password}, (err, docs) => {
            if (docs == null) {
                result.status = 1;
                result.message ="用户名或密码错误"
                res.send(result);
                client.close();
            }else{
                req.session.loginName =  param.username;
                client.close();
                res.send(result);
            }
        });
    });
}

exports.Logout = (req,res)=>{
    req.session.loginName = null;
    res.send('<script>location.href="/account/login"</script>');
}